/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufba.lasid.v1.jitclouds.mda.providers;

import br.ufba.lasid.v1.jitclouds.mda.MDA;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.BasicCloud;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.ICloud;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.IDataCenter;
import java.util.List;
import java.util.Properties;

/**
 *
 * @author aliriosa
 */
public class CloudProvider implements IProvider<ICloud> {

    @Override
    public ICloud create(Properties options) throws Exception {
        
        MDA.debug("Preparing to create a new cloud ... ");
        
        BasicCloud cloud = new BasicCloud();
                
        String idCloud = MDA.Configuration.getString("mda.cloud.id");
        
        if(idCloud != null){
            String dscCloud = MDA.Configuration.getString(idCloud + ".description");
            String eptCloud = MDA.Configuration.getString(idCloud + ".endpoint");
            
            cloud.setID(idCloud);
            cloud.setDescription(dscCloud);
            cloud.setEndpoint(eptCloud);

            MDA.debug(
                "Preparing cloud " +  cloud.getID() + " [" + cloud.getDescription() + "] " +
                "listening at " + cloud.getEndpoint()
            );
                        
            List DCsID = MDA.Configuration.getList(idCloud + ".dc.id");
            
            MDA.debug("Loading the Data Centers of Cloud " + cloud.getID() + ".");

            for(Object DCID : DCsID){
                
                MDA.BaseOptions.setProperty(MDA.DCTag, (String)DCID);
                
                String DCProvider = MDA.Configuration.getString(DCID + ".provider");

                Class _class = Class.forName(DCProvider);
                
                IDataCenter dc = MDA.create(_class, options);            
                
                cloud.addDC(dc);
                
            }

            MDA.debug(cloud.size() + " data centers were loaded in " + cloud.getID() + ".");            
            MDA.debug("The Data Centers of Cloud " + cloud.getID() + " has been successfully loaded.");            
            MDA.debug("Cloud " + cloud.getID() + " was successfully loaded.");
            
        }else{
            MDA.debug("MDA didn't find a base cloud configuration.");
            MDA.debug("Cloud creation returns a empty cloud.");
        }
        
        return cloud;
    }
    
}
